﻿package coreFx{
	
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	
	public class Mild extends MovieClip{
	
	//Const
		//Direction
		public static var _RIGHT:int = 0;
		public static var _LEFT:int = 1;
		public static var _UP:int = 2;
		public static var _DOWN:int = 3;		
	
	
	
	//Attrib
	var state0Instance:State0 = new State0(2,stage);
	var state1Instance:State1 = new State1(3,stage);
	var state2Instance:State2 = new State2(3,stage);
	var state3Instance:State3 = new State3(2,stage);
	var state4Instance:State4 = new State4(8,stage);
	var state5Instance:State5 = new State5(3,stage);
	var stateFinishInstance:StateFinish = new StateFinish(stage);
	
	var originClock:Timer = new Timer(0,0);
	var state0Clock:Timer = new Timer(0,0);
	var state1Clock:Timer = new Timer(0,0);
	var state2Clock:Timer = new Timer(0,0);
	var state3Clock:Timer = new Timer(0,0);
	var state4Clock:Timer = new Timer(0,0);
	var state5Clock:Timer = new Timer(0,0);
	
	
    var	state0_OneTimeFlag:Boolean = false;
	var	state1_OneTimeFlag:Boolean = false;
	var	state2_OneTimeFlag:Boolean = false;
	var	state3_OneTimeFlag:Boolean = false;
	var	state4_OneTimeFlag:Boolean = false;
	var	state5_OneTimeFlag:Boolean = false;
	
	
	var forward:Boolean = false;
	var customerList:Array;
	
	
	public function Mild(){
		
		trace("Hello world this is mild");
		customerList= new Array();
		
		stage.addChild(state0Instance);
		stage.addChild(state1Instance);
		stage.addChild(state2Instance);
		stage.addChild(state3Instance);
		stage.addChild(state4Instance);
		stage.addChild(state5Instance);
		stage.addChild(stateFinishInstance);
		addEventListener(Event.ENTER_FRAME,Trigger);
	
	}
	
	
	public function Trigger(e:Event){
		
		var count:Number = 0;
		//if(customerList.length > 40){ trace('#####Stop#####');return 0;/* Stop generate customer */}
		//###############################################################	
		//##################### Customer generater ######################
		//###############################################################
		if(!forward)
			{
				
				var max = 10;
				var min = 1;
				
				var rx = Math.round(Math.random()*(max-min)+min);
				if(rx<=2)
				{	
					//20 sec. prop 0.2
					trace('20 sec');
					originClock = new Timer(20000,1);
				}
				else if(rx<=5)
				{
					//15 sec. prop 0.3
					trace('15 sec');
					originClock = new Timer(15000,1);
				}
				else if(rx <=10)
				{
					//10 sec. prop 0.5
					trace('10 sec');
					originClock = new Timer(10000,1);
				}
				//originClock = new Timer(400,1);
				originClock.start();
				originClock.addEventListener(TimerEvent.TIMER,releaseCustomer);
				forward = true;
				
				if(count < customerList.length){
					count = customerList.length;
					trace('----> Customer No. '+ count + ' created.');	
				}
			}
		

			
		//###############################################################	
		//##################### Automate Running ########################
		//###############################################################
		//Automate for	
		for(var i=0; i<customerList.length; i++){
					
			//##################### Start Point to State 0 ########################	
			if(customerList[i].target == "state0"){
				//customerList[i].walk(Mild._DOWN);
				if(customerList[i].x > state0Instance.x )customerList[i].walk(Mild._LEFT);
				if(customerList[i].y < state0Instance.y )customerList[i].walk(Mild._DOWN);
				
				if(customerList[i].x < state0Instance.x )customerList[i].walk(Mild._RIGHT);
				if(customerList[i].y > state0Instance.y )customerList[i].walk(Mild._UP);
				
				
				
				//Customer reach state
				if(customerList[i].y == state0Instance.y && customerList[i].x == state0Instance.x){
					customerList[i].target = "wait";
					customerList[i].wait();
					customerList[i].y = state0Instance.y+20;
					state0Instance._push(customerList[i]);
				}
				
				
			}//end if stage0
			
			//State has customer
				if(state0Instance.InQueue.length >0){
					/*if(!state0_OneTimeFlag){
						//trace('get in0');
						var delayOfState0:Number = 1000;//in ms
						state0Clock = new Timer(delayOfState0,1);
						state0Clock.start();
						state0_OneTimeFlag = true;
						state0Clock.addEventListener(TimerEvent.TIMER,state0_Pop);
						//trace('pass0')
					}*/
					trace('InQueue of 0 not Empty'+state0Instance.InQueue.length);
					
					var childCount:Number = state0Instance.staffList.length;
					for(var x = 0;x<childCount;x++){
						
						var staffx:Staff = state0Instance.staffList[x];
						if(staffx.InQueue.length == 0){
							staffx._push(state0Instance._popToChild());
						}
					}//end for
				
				}//end if
				
			
			
			
			
			
			//##################### Start Point to State 1 ########################
			if(customerList[i].target == "state1"){
				if(customerList[i].x > state1Instance.x )customerList[i].walk(Mild._LEFT);
				if(customerList[i].y < state1Instance.y )customerList[i].walk(Mild._DOWN);
				
				if(customerList[i].x < state1Instance.x )customerList[i].walk(Mild._RIGHT);
				if(customerList[i].y > state1Instance.y )customerList[i].walk(Mild._UP);
				
				
				//Customer reach state
				if(customerList[i].y == state1Instance.y && customerList[i].x == state1Instance.x ){
					customerList[i].target = "wait";
					customerList[i].y = state1Instance.y+20;
					state1Instance._push(customerList[i]);
			
				}
				

			}//end if stage1
			
				//State has customer
				if(state1Instance.InQueue.length >0){
					/*if(!state1_OneTimeFlag){
						//trace('get in1 ');
						var delayOfState1:Number = 1200;//in ms
						state1Clock = new Timer(delayOfState1,1);
						state1Clock.start();
						state1_OneTimeFlag = true;
						state1Clock.addEventListener(TimerEvent.TIMER,state1_Pop);
						//trace('pass1')
					}*/
					
					var childCount:Number = state1Instance.staffList.length;
					for(var x = 0;x<childCount;x++){
						var staffx:Staff = state1Instance.staffList[x];
						if(staffx.InQueue.length == 0){
							staffx._push(state1Instance._popToChild());
						}
					}//end for
				
				}//end if			
			
			//##################### Start Point to State 2 ########################
			if(customerList[i].target == "state2"){
				if(customerList[i].x > state2Instance.x )customerList[i].walk(Mild._LEFT);
				if(customerList[i].y < state2Instance.y )customerList[i].walk(Mild._DOWN);
				
				if(customerList[i].x < state2Instance.x )customerList[i].walk(Mild._RIGHT);
				if(customerList[i].y > state2Instance.y )customerList[i].walk(Mild._UP);

				//Customer reach state
				if(customerList[i].y == state2Instance.y && customerList[i].x == state2Instance.x ){
					customerList[i].target = "wait";
					customerList[i].y = state2Instance.y+20;
					state2Instance._push(customerList[i]);
				}
				
			}//end if stage2
			
				//State has customer
				if(state2Instance.InQueue.length >0){
					/*if(!state2_OneTimeFlag){
						//trace('get in2 ');
						var delayOfState2:Number = 1000;//in ms
						state2Clock = new Timer(delayOfState2,1);
						state2Clock.start();
						state2_OneTimeFlag = true;
						state2Clock.addEventListener(TimerEvent.TIMER,state2_Pop);
						//trace('pass2')
					}*/
					var childCount:Number = state2Instance.staffList.length;
					for(var x = 0;x<childCount;x++){
						var staffx:Staff = state2Instance.staffList[x];
						if(staffx.InQueue.length == 0){
							staffx._push(state2Instance._popToChild());
						}
					}//end for
				}//end if			
			
			
			//##################### Start Point to State 3 ########################
			if(customerList[i].target == "state3"){
				//trace("there is 3 ")
				if(customerList[i].x > state3Instance.x )customerList[i].walk(Mild._LEFT);
				if(customerList[i].y < state3Instance.y )customerList[i].walk(Mild._DOWN);
				
				if(customerList[i].x < state3Instance.x )customerList[i].walk(Mild._RIGHT);
				if(customerList[i].y > state3Instance.y )customerList[i].walk(Mild._UP);
				//Customer reach state
				if(customerList[i].y == state3Instance.y && customerList[i].x == state3Instance.x ){
					customerList[i].target = "wait";
					customerList[i].y = state3Instance.y+20;
					state3Instance._push(customerList[i]);
				}
							
			}//end if stage3
			
				//State has customer
				if(state3Instance.InQueue.length >0){
					/*if(!state3_OneTimeFlag){
						trace('get in3 ');
						var delayOfState3:Number = 1500;//in ms
						state3Clock = new Timer(delayOfState3,1);
						state3Clock.start();
						state3_OneTimeFlag = true;
						state3Clock.addEventListener(TimerEvent.TIMER,state3_Pop);
						trace('pass3')
					}*/
					var childCount:Number = state3Instance.staffList.length;
					for(var x = 0;x<childCount;x++){
						var staffx:Staff = state3Instance.staffList[x];
						if(staffx.InQueue.length == 0){
							staffx._push(state3Instance._popToChild());
						}
					}//end for
				}//end if				
			
			
			//##################### Start Point to State 4 ########################
			if(customerList[i].target == "state4"){
				if(customerList[i].x > state4Instance.x )customerList[i].walk(Mild._LEFT);
				if(customerList[i].y < state4Instance.y )customerList[i].walk(Mild._DOWN);
				
				if(customerList[i].x < state4Instance.x )customerList[i].walk(Mild._RIGHT);
				if(customerList[i].y > state4Instance.y )customerList[i].walk(Mild._UP);
				//Customer reach state
				if(customerList[i].y == state4Instance.y && customerList[i].x == state4Instance.x){
					customerList[i].target = "wait";
					customerList[i].y = state4Instance.y+20;
					state4Instance._push(customerList[i]);
				}
				
			}//end if stage4
			
				//State has customer
				if(state4Instance.InQueue.length >0){
					/*if(!state4_OneTimeFlag){
						trace('get in4 ');
						var delayOfState4:Number = 2000;//in ms
						state4Clock = new Timer(delayOfState4,1);
						state4Clock.start();
						state4_OneTimeFlag = true;
						state4Clock.addEventListener(TimerEvent.TIMER,state4_Pop);
						trace('pass4')
					}*/
					var childCount:Number = state4Instance.staffList.length;
					for(var x = 0;x<childCount;x++){
						var staffx:Staff = state4Instance.staffList[x];
						if(staffx.InQueue.length == 0){
							staffx._push(state4Instance._popToChild());
						}
					}//end for				
				}//end if			
			
			
			
			//##################### Start Point to State 5 ########################
			if(customerList[i].target == "state5"){
				if(customerList[i].x > state5Instance.x )customerList[i].walk(Mild._LEFT);
				if(customerList[i].y < state5Instance.y )customerList[i].walk(Mild._DOWN);
				
				if(customerList[i].x < state5Instance.x )customerList[i].walk(Mild._RIGHT);
				if(customerList[i].y > state5Instance.y )customerList[i].walk(Mild._UP);
				//Customer reach state
				if(customerList[i].y == state5Instance.y && customerList[i].x == state5Instance.x){
					customerList[i].target = "wait";
					customerList[i].y = state5Instance.y+20;
					state5Instance._push(customerList[i]);
				}
				
			}//end if stage5
			
				//State has customer
				if(state5Instance.InQueue.length >0){
					/*if(!state5_OneTimeFlag){
						trace('get in5 ');
						var delayOfState5:Number = 2500;//in ms
						state5Clock = new Timer(delayOfState5,1);
						state5Clock.start();
						state5_OneTimeFlag = true;
						state5Clock.addEventListener(TimerEvent.TIMER,state5_Pop);
						trace('pass5')
					}*/
					var childCount:Number = state5Instance.staffList.length;
					for(var x = 0;x<childCount;x++){
						var staffx:Staff = state5Instance.staffList[x];
						if(staffx.InQueue.length == 0){
							staffx._push(state5Instance._popToChild());
						}
					}//end for						
				}//end if			
			
					//##################### Start Point to State Finish ########################
			if(customerList[i].target == "stateFinish"){
				if(customerList[i].x > stateFinishInstance.x )customerList[i].walk(Mild._LEFT);
				if(customerList[i].y < stateFinishInstance.y )customerList[i].walk(Mild._DOWN);
				
				if(customerList[i].x < stateFinishInstance.x )customerList[i].walk(Mild._RIGHT);
				if(customerList[i].y > stateFinishInstance.y )customerList[i].walk(Mild._UP);
				//Customer reach state
				
				if(customerList[i].y == stateFinishInstance.y && customerList[i].x == stateFinishInstance.x){
					customerList[i].target = "wait";
					//customerList[i].y = stateFinishInstance.y+20;
					stateFinishInstance._push(customerList[i]);
					trace('Finished '+ stateFinishInstance.InQueue.length + ' man.' );
				}
				//trace('State Finish queue' + stateFinishInstance.InQueue.length);
			}//end if stage5
			
			
			
			
			
			
			
			
			
			
		}//End Automate for	
	}//End Trigger function 
	
	
	
	
	//############################### Public method ############################
	public function state0_Pop(e:TimerEvent){
		trace('shell pop 0 ')
		state0Instance._pop();
		state0Clock.removeEventListener(TimerEvent.TIMER,state0_Pop);
		state0_OneTimeFlag = false;
	}
	
	public function state1_Pop(e:TimerEvent){
		trace('shell pop 1 ')
		state1Instance._pop();
		state1Clock.removeEventListener(TimerEvent.TIMER,state1_Pop);
		state1_OneTimeFlag = false;
	}
	
	public function state2_Pop(e:TimerEvent){
		trace('shell pop 2 ')
		state2Instance._pop();
		state2Clock.removeEventListener(TimerEvent.TIMER,state2_Pop);
		state2_OneTimeFlag = false;
	}
	
	public function state3_Pop(e:TimerEvent){
		trace('shell pop 3 ')
		state3Instance._pop();
		state3Clock.removeEventListener(TimerEvent.TIMER,state3_Pop);
		state3_OneTimeFlag = false;
	}
	
	public function state4_Pop(e:TimerEvent){
		trace('shell pop 4 ')
		state4Instance._pop();
		state4Clock.removeEventListener(TimerEvent.TIMER,state4_Pop);
		state4_OneTimeFlag = false;
	}
	
	public function state5_Pop(e:TimerEvent){
		trace('shell pop 5 ')
		state5Instance._pop();
		state5Clock.removeEventListener(TimerEvent.TIMER,state5_Pop);
		state5_OneTimeFlag = false;
	}
	
	
	
	
	
	public function releaseCustomer(e:TimerEvent){
		if(customerList.length<40){
		var customer:Customer = new Customer();
			customerList.push(customer);
			customer.x=customer.y=0;
			stage.addChild(customer);
		
		}
			originClock.removeEventListener(TimerEvent.TIMER,releaseCustomer);
			forward = false;
	}
	
	/*public function RandomRange(min:Number,max:Number):Number
	{
			var scale:Number = max-min;
			return Math.random()*(max-min)+min;
	}*/
	
	
}//End Class

}//End Package